package com.itboye.lottery.utils.http;


import android.text.TextUtils;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.TypeReference;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by wangenning on 16/1/8.
 */
public class FastOk {

    public FastOk() {
    }

    /**
     * 第一种格式：
     * {"code":1,"data":[{"children":[{"code":"11","depth":2,"id":2,"isclose":0,"name":"数学史","parent_code":"110","state":"","status":0,"total":0},{"code":"14","depth":2,"id":3,"isclose":0,"name":"数理逻辑与数学基础","parent_code":"110","state":"","status":0,"total":0},{"code":"1410","depth":2,"id":4,"isclose":0,"name":"演绎逻辑学(亦称符号逻辑学)","parent_code":"110","state":"","status":0,"total":0},{"code":"17","depth":2,"id":5,"isclose":0,"name":"数论","parent_code":"110","state":"","status":0,"total":0},{"code":"qeqwe","depth":2,"id":16,"isclose":0,"name":"qqqq","parent_code":"110","state":"","status":0,"total":0}],"code":"110","depth":1,"id":1,"isclose":0,"name":"数学","parent_code":"1","state":"closed","status":0,"total":0},{"children":[{"code":"10","depth":2,"id":7,"isclose":0,"name":"信息科学与系统科学基础学科","parent_code":"120","state":"","status":0,"total":0},{"code":"1010","depth":2,"id":8,"isclose":0,"name":"信息论","parent_code":"120","state":"","status":0,"total":0},{"code":"1099","depth":2,"id":9,"isclose":0,"name":" 信息科学与系统科学基础学科其他学科","parent_code":"120","state":"","status":0,"total":0}],"code":"120","depth":1,"id":6,"isclose":0,"name":"信息科学与系统科学","parent_code":"1","state":"closed","status":0,"total":0},{"children":[{"code":"2534","depth":2,"id":11,"isclose":0,"name":"多相流体力学","parent_code":"130","state":"","status":0,"total":0},{"code":"2537","depth":2,"id":12,"isclose":0,"name":"渗流力学","parent_code":"130","state":"","status":0,"total":0}],"code":"130","depth":1,"id":10,"isclose":0,"name":"力学","parent_code":"1","state":"closed","status":0,"total":0},{"children":[{"code":"121","depth":2,"id":18,"isclose":0,"name":"qqq1","parent_code":"111","state":"","status":0,"total":0}],"code":"111","depth":1,"id":17,"isclose":0,"name":"物理学","parent_code":"1","state":"closed","status":0,"total":0},{"children":[{"code":"weweew","depth":2,"id":27,"isclose":0,"name":"ewewwwewe","parent_code":"ewe","state":"","status":0,"total":0}],"code":"ewe","depth":1,"id":26,"isclose":0,"name":"生物学","parent_code":"1","state":"closed","status":0,"total":0},{"children":[{"code":"40101","depth":2,"id":76,"isclose":0,"name":"教育学","parent_code":"401","state":"","status":0,"total":0},{"code":"40102","depth":2,"id":77,"isclose":0,"name":"科学教育","parent_code":"401","state":"","status":0,"total":0},{"code":"40103","depth":2,"id":78,"isclose":0,"name":"人文教育","parent_code":"401","state":"","status":0,"total":0},{"code":"40104","depth":2,"id":79,"isclose":0,"name":"教育技术学","parent_code":"401","state":"","status":0,"total":0}],"code":"401","depth":1,"id":75,"isclose":0,"name":"教育学类","parent_code":"1","state":"closed","status":0,"total":0},{"children":[{"code":"50101","depth":2,"id":81,"isclose":0,"name":"文艺学","parent_code":"501","state":"","status":0,"total":0},{"code":"50102","depth":2,"id":82,"isclose":0,"name":"语言学及应用语言学","parent_code":"501","state":"","status":0,"total":0},{"code":"50103","depth":2,"id":83,"isclose":0,"name":"汉语言文字学","parent_code":"501","state":"","status":0,"total":0}],"code":"501","depth":1,"id":80,"isclose":0,"name":"中国语言文学","parent_code":"1","state":"closed","status":0,"total":0}],"total":7}
     *
     * @param string
     * @param cls
     * @return
     */
    public static <T> T get(String string, Class<T> cls) {
        T t = null;
        if(string == null){
            return null;
        }
        try {
            t =  JSONObject.parseObject(string, cls);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return t;
    }

    public static <T> T get(String string, TypeReference<T> type) {
        T t = null;
        if(string == null){
            return null;
        }
        try {
            t =  JSONObject.parseObject(string,type);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return t;
    }

    /**
     * 第一种格式：
     * {"code":1,"data":[{"children":[{"code":"11","depth":2,"id":2,"isclose":0,"name":"数学史","parent_code":"110","state":"","status":0,"total":0},{"code":"14","depth":2,"id":3,"isclose":0,"name":"数理逻辑与数学基础","parent_code":"110","state":"","status":0,"total":0},{"code":"1410","depth":2,"id":4,"isclose":0,"name":"演绎逻辑学(亦称符号逻辑学)","parent_code":"110","state":"","status":0,"total":0},{"code":"17","depth":2,"id":5,"isclose":0,"name":"数论","parent_code":"110","state":"","status":0,"total":0},{"code":"qeqwe","depth":2,"id":16,"isclose":0,"name":"qqqq","parent_code":"110","state":"","status":0,"total":0}],"code":"110","depth":1,"id":1,"isclose":0,"name":"数学","parent_code":"1","state":"closed","status":0,"total":0},{"children":[{"code":"10","depth":2,"id":7,"isclose":0,"name":"信息科学与系统科学基础学科","parent_code":"120","state":"","status":0,"total":0},{"code":"1010","depth":2,"id":8,"isclose":0,"name":"信息论","parent_code":"120","state":"","status":0,"total":0},{"code":"1099","depth":2,"id":9,"isclose":0,"name":" 信息科学与系统科学基础学科其他学科","parent_code":"120","state":"","status":0,"total":0}],"code":"120","depth":1,"id":6,"isclose":0,"name":"信息科学与系统科学","parent_code":"1","state":"closed","status":0,"total":0},{"children":[{"code":"2534","depth":2,"id":11,"isclose":0,"name":"多相流体力学","parent_code":"130","state":"","status":0,"total":0},{"code":"2537","depth":2,"id":12,"isclose":0,"name":"渗流力学","parent_code":"130","state":"","status":0,"total":0}],"code":"130","depth":1,"id":10,"isclose":0,"name":"力学","parent_code":"1","state":"closed","status":0,"total":0},{"children":[{"code":"121","depth":2,"id":18,"isclose":0,"name":"qqq1","parent_code":"111","state":"","status":0,"total":0}],"code":"111","depth":1,"id":17,"isclose":0,"name":"物理学","parent_code":"1","state":"closed","status":0,"total":0},{"children":[{"code":"weweew","depth":2,"id":27,"isclose":0,"name":"ewewwwewe","parent_code":"ewe","state":"","status":0,"total":0}],"code":"ewe","depth":1,"id":26,"isclose":0,"name":"生物学","parent_code":"1","state":"closed","status":0,"total":0},{"children":[{"code":"40101","depth":2,"id":76,"isclose":0,"name":"教育学","parent_code":"401","state":"","status":0,"total":0},{"code":"40102","depth":2,"id":77,"isclose":0,"name":"科学教育","parent_code":"401","state":"","status":0,"total":0},{"code":"40103","depth":2,"id":78,"isclose":0,"name":"人文教育","parent_code":"401","state":"","status":0,"total":0},{"code":"40104","depth":2,"id":79,"isclose":0,"name":"教育技术学","parent_code":"401","state":"","status":0,"total":0}],"code":"401","depth":1,"id":75,"isclose":0,"name":"教育学类","parent_code":"1","state":"closed","status":0,"total":0},{"children":[{"code":"50101","depth":2,"id":81,"isclose":0,"name":"文艺学","parent_code":"501","state":"","status":0,"total":0},{"code":"50102","depth":2,"id":82,"isclose":0,"name":"语言学及应用语言学","parent_code":"501","state":"","status":0,"total":0},{"code":"50103","depth":2,"id":83,"isclose":0,"name":"汉语言文字学","parent_code":"501","state":"","status":0,"total":0}],"code":"501","depth":1,"id":80,"isclose":0,"name":"中国语言文学","parent_code":"1","state":"closed","status":0,"total":0}],"total":7}
     *
     * @param string
     * @param cls
     * @return
     */
    public static <T> List<T> getList(String string, Class<T> cls) {
        List<T> t = new ArrayList<>();
        if(string == null){
            return t;
        }
        try {
            t = JSONObject.parseArray(string, cls);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return t;
    }


    public static String string(Object obj) {

        try {
            return JSONObject.toJSONString(obj);
        } catch (Exception e) {

        }
        return "";

    }


    public static boolean isEmpty(List<? extends Object> list){
        if(list == null || list.size() == 0){
            return true;
        }
        return false;

    }

    public static String subStr(String content, int sub_length, String suffix){
        if(TextUtils.isEmpty(content)){
            return "";
        }
        if(content.length() > sub_length){
            content = content.substring(0,sub_length) + suffix;
        }else{
            return content;
        }
        return content;

    }

    public static JSONArray getArray(String success_notice) {
        try {

            JSONArray arr = JSONObject.parseArray(success_notice);
            return arr;
        } catch (Exception e) {
            return null;
        }

    }

}
